﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace Phoenix.Xna.Framework.Input.Mice
{
    /// <summary>
    /// Tracks the state of the mouse.
    /// </summary>
    internal sealed class MouseStateManager : InputStateManager<MouseState>
    {
        private class NestedSingleton
        {
            internal static readonly MouseStateManager Singleton = new MouseStateManager();

            static NestedSingleton() { }
        }

        /// <summary>
        /// Gets a singleton instance of the MouseStateManager.
        /// </summary>
        public static MouseStateManager Instance
        {
            get { return NestedSingleton.Singleton; }
        }

        /// <summary>
        /// Gets the current value of the scrollwheel.
        /// </summary>
        public int CurrentScrollWheelValue
        {
            get { return CurrentState.ScrollWheelValue; }
        }

        /// <summary>
        /// Gets the previous value of the scrollwheel.
        /// </summary>
        public int PreviousScrollWheelValue
        {
            get { return PreviousState.ScrollWheelValue; }
        }

        /// <summary>
        /// Gets the current change in position of the scrollwheel.
        /// </summary>
        public int CurrentScrollWheelDelta
        {
            get { return CurrentScrollWheelValue - PreviousScrollWheelValue; }
        }

        /// <summary>
        /// Gets the previous change in position of the scrollwheel.
        /// </summary>
        public int PreviousScrollWheelDelta { get; private set; }

        /// <summary>
        /// Gets the current position of the mouse.
        /// </summary>
        public Vector2 CurrentPosition
        {
            get { return new Vector2(CurrentState.X, CurrentState.Y); }
        }

        /// <summary>
        /// Gets the previous position of the mouse.
        /// </summary>
        public Vector2 PreviousPosition
        {
            get { return new Vector2(PreviousState.X, PreviousState.Y); }
        }

        /// <summary>
        /// Gets the current change in position of the mouse.
        /// </summary>
        public Vector2 CurrentPositionDelta
        {
            get { return CurrentPosition - PreviousPosition; }
        }

        /// <summary>
        /// Gets the previous change in position of the mouse.
        /// </summary>
        public Vector2 PreviousPositionDelta { get; private set; }

        private MouseStateManager()
        {
            CurrentState = Mouse.GetState();
        }

        #region InputStateManger Members

        /// <summary>
        /// Updates the states for the input device.
        /// </summary>
        public override void Update()
        {
            PreviousState = CurrentState;
            PreviousScrollWheelDelta = CurrentScrollWheelDelta;
            PreviousPositionDelta = CurrentPositionDelta;
            CurrentState = Mouse.GetState();
            base.Update();
        }

        #endregion
    }
}
